package com.ybkj.chat.ui.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.ybkj.chat.R;

/**
 * 清空群聊天记录
 *
 * @author wyq
 * @date 2019/2/27
 */

public class PpwClearChatRecord extends PopupWindow implements View.OnClickListener {

    private LayoutInflater mInflater;
    private View mView;

    private TextView mComfirm, mTvCancel;
    private RadioGroup radioGroup;
    private RadioButton onehour, threehour, sixhour, oneday, threeday, threedayMore, alltime;

    private OnItemClickListener onItemClickListener;

    private Context mContext;
    private Activity mActivity;

    public PpwClearChatRecord(Context context) {
        super(context);
        this.mContext = context;
        mInflater = LayoutInflater.from(context);
        init();
        setPopwindow();
    }

    private void init() {
        mView = mInflater.inflate(R.layout.pop_clear_chat_record, null);
        mComfirm = (TextView) mView.findViewById(R.id.confirm);
        onehour = (RadioButton) mView.findViewById(R.id.one_hour);
        threehour = (RadioButton) mView.findViewById(R.id.three_hour);
        sixhour = (RadioButton) mView.findViewById(R.id.six_hour);
        oneday = (RadioButton) mView.findViewById(R.id.one_day);
        threeday = (RadioButton) mView.findViewById(R.id.three_day);
        threedayMore = (RadioButton) mView.findViewById(R.id.three_day_more);
        alltime = (RadioButton) mView.findViewById(R.id.all_time);
        mTvCancel = (TextView) mView.findViewById(R.id.cancel);

        mComfirm.setOnClickListener(this);
        mTvCancel.setOnClickListener(this);
    }

    private void setPopwindow() {
        this.setContentView(mView);
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        // this.setAnimationStyle(R.style.ppw_select_picture);
        this.setBackgroundDrawable(new BitmapDrawable());
        this.setFocusable(true);
        this.setOutsideTouchable(true);

        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                if (mActivity != null) {
                    setBackgroundAlpha(mActivity, 1f);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        if (onItemClickListener != null) {
            onItemClickListener.onItemClick(v);
        }
    }

    /**
     * 设置页面的透明度
     *
     * @param bgAlpha 1表示不透明
     */
    public void setBackgroundAlpha(Activity activity, float bgAlpha) {
        this.mActivity = activity;
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        activity.getWindow().setAttributes(lp);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View v);
    }

}
